<cfcomponent extends="BaseTest" output="false">
	<cffunction name="setup" access="public" returntype="void" output="false">
		<cfset var mockESAPI = createObject('component', "mockObject") />
		<cfset var mockESAPIMethods = createObject('component', 'LoggerTestMethods') />
		<cfset var mockSessionFacade = "" />
		<cfset var mockAuthenticator = "" />
		<cfset var mockUser = "" />
		<cfset var securityConfiguration = "" />
		<cfset var encoder = "" />

		<!--- create the getter and setter for phoney objects--->
		<cfset injectMethod(mockESAPI, mockESAPIMethods, "setSessionFacade") />
		<cfset injectMethod(mockESAPI, mockESAPIMethods, "setAuthenticator") />
		<cfset injectMethod(mockESAPI, mockESAPIMethods, "setSecurityConfiguration") />
		<cfset injectMethod(mockESAPI, mockESAPIMethods, "setEncoder") />
		<cfset injectMethod(mockESAPI, mockESAPIMethods, "sessionFacade") />
		<cfset injectMethod(mockESAPI, mockESAPIMethods, "authenticator") />
		<cfset injectMethod(mockESAPI, mockESAPIMethods, "securityConfiguration") />
		<cfset injectMethod(mockESAPI, mockESAPIMethods, "encoder") />

		<!--- Create mockEncoder --->
		<cfset mockEncoder = createObject('component', "mockObject") />
		<cfset injectMethod(mockEncoder, mockESAPIMethods, "encodeForHTML") />

		<!--- Create a phoney sessionFacade and inject the getProperty Method --->
		<cfset mockSessionFacade = createObject('component', "mockObject") />
		<cfset injectMethod(mockSessionFacade, mockESAPIMethods, "getProperty") />

		<!--- Create mockSecurityConfiguration --->
		<cfset mockSecurityConfiguration = createObject('component', "mockObject") />
		<cfset injectMethod(mockSecurityConfiguration, mockESAPIMethods, "getProperty") />

		<!--- Create mockUser --->
		<cfset mockUser = createObject('component', "mockObject") />
		<cfset injectMethod(mockUser, mockESAPIMethods, "getUsername") />
		<cfset injectMethod(mockUser, mockESAPIMethods, "getLastHostAddress") />

		<!--- Create mockAuthenticator --->
		<cfset mockAuthenticator = createObject('component', "mockObject") />
		<cfset injectMethod(mockAuthenticator, mockESAPIMethods, "setUser") />
		<cfset injectMethod(mockAuthenticator, mockESAPIMethods, "getCurrentUser") />

		<!--- inject --->
		<cfset mockAuthenticator.setUser(mockUser) />
		<cfset mockESAPI.setSessionFacade(mockSessionFacade) />
		<cfset mockESAPI.setAuthenticator(mockAuthenticator) />
		<cfset mockESAPI.setSecurityConfiguration(mockSecurityConfiguration) />
		<cfset mockESAPI.setEncoder(mockEncoder) />

		<!--- Create logger with some default values and pass in the mockESAPI --->
		<cfset variables.testLogger = createObject('component', root & 'cfesapi.org.owasp.esapi.Logger').init("TestLogger", "TRACE", mockESAPI) />
	</cffunction>



	<cffunction name="LoggerShouldReturnItself" access="public" returntype="void" output="false">
		<cfset assertSame(testLogger, testLogger)/>
		<cfset assertIsTypeOf(testLogger, root & 'cfesapi.org.owasp.esapi.Logger')>
		<cfset assertIsTypeOf(testLogger,'WEB-INF.cftags.component') />
	</cffunction>

	<cffunction name="LoggerShouldAllowLogLevelChanges" access="public" returntype="void" output="false">
		<!--- Set testLogger to ALL and test --->
		<cfset testLogger.setLevel("All") />

		<cfset assertEquals(testLogger.getLevel(), "ALL", "getLevel() Fail on All") />

		<cfset assertTrue(testLogger.isTraceEnabled(), "isTraceEnabled() Fail on Level: All") />
		<cfset assertTrue(testLogger.isDebugEnabled(), "isDebugEnabled() Fail on Level: All") />
		<cfset assertTrue(testLogger.isInfoEnabled(), "isInfoEnabled() Fail on Level: All") />
		<cfset assertTrue(testLogger.isEnabledFor("WARN"), "isEnabledFor('WARN') Fail on Level: All") />
		<cfset assertTrue(testLogger.isEnabledFor("ERROR"), "isEnabledFor('ERROR') Fail on Level: All") />
		<cfset assertTrue(testLogger.isEnabledFor("FATAL"), "isEnabledFor('FATAL') Fail on Level: All") />

		<!--- Set Logger to TRACE and Test --->
		<cfset testLogger.setLevel("TRACE") />

		<cfset assertEquals(testLogger.getLevel(), "TRACE", "getLevel() Fail on TRACE") />

		<cfset assertTrue(testLogger.isTraceEnabled(), "isTraceEnabled() Fail on Level: TRACE") />
		<cfset assertTrue(testLogger.isDebugEnabled(), "isDebugEnabled() Fail on Level: TRACE") />
		<cfset assertTrue(testLogger.isInfoEnabled(), "isInfoEnabled() Fail on Level: TRACE") />
		<cfset assertTrue(testLogger.isEnabledFor("WARN"), "isEnabledFor('WARN') Fail on Level: TRACE") />
		<cfset assertTrue(testLogger.isEnabledFor("ERROR"), "isEnabledFor('ERROR') Fail on Level: TRACE") />
		<cfset assertTrue(testLogger.isEnabledFor("FATAL"), "isEnabledFor('FATAL') Fail on Level: TRACE") />

		<!--- Set Logger to DEBUG and Test --->
		<cfset testLogger.setLevel("DEBUG") />

		<cfset assertEquals(testLogger.getLevel(), "DEBUG", "getLevel() Fail on DEBUG") />

		<cfset assertFalse(testLogger.isTraceEnabled(), "isTraceEnabled() Fail on Level: DEBUG") />
		<cfset assertTrue(testLogger.isDebugEnabled(), "isDebugEnabled() Fail on Level: DEBUG") />
		<cfset assertTrue(testLogger.isInfoEnabled(), "isInfoEnabled() Fail on Level: DEBUG") />
		<cfset assertTrue(testLogger.isEnabledFor("WARN"), "isEnabledFor('WARN') Fail on Level: DEBUG") />
		<cfset assertTrue(testLogger.isEnabledFor("ERROR"), "isEnabledFor('ERROR') Fail on Level: DEBUG") />
		<cfset assertTrue(testLogger.isEnabledFor("FATAL"), "isEnabledFor('FATAL') Fail on Level: DEBUG") />

		<!--- Set Logger to INFO and Test --->
		<cfset testLogger.setLevel("INFO") />

		<cfset assertEquals(testLogger.getLevel(), "INFO", "getLevel() Fail on INFO") />

		<cfset assertFalse(testLogger.isTraceEnabled(), "isTraceEnabled() Fail on Level: INFO") />
		<cfset assertFalse(testLogger.isDebugEnabled(), "isDebugEnabled() Fail on Level: INFO") />
		<cfset assertTrue(testLogger.isInfoEnabled(), "isInfoEnabled() Fail on Level: INFO") />
		<cfset assertTrue(testLogger.isEnabledFor("WARN"), "isEnabledFor('WARN') Fail on Level: INFO") />
		<cfset assertTrue(testLogger.isEnabledFor("ERROR"), "isEnabledFor('ERROR') Fail on Level: INFO") />
		<cfset assertTrue(testLogger.isEnabledFor("FATAL"), "isEnabledFor('FATAL') Fail on Level: INFO") />

		<!--- Set Logger to WARN and Test --->
		<cfset testLogger.setLevel("WARN") />

		<cfset assertEquals(testLogger.getLevel(), "WARN", "getLevel() Fail on WARN") />

		<cfset assertFalse(testLogger.isTraceEnabled(), "isTraceEnabled() Fail on Level: WARN") />
		<cfset assertFalse(testLogger.isDebugEnabled(), "isDebugEnabled() Fail on Level: WARN") />
		<cfset assertFalse(testLogger.isInfoEnabled(), "isInfoEnabled() Fail on Level: WARN") />
		<cfset assertTrue(testLogger.isEnabledFor("WARN"), "isEnabledFor('WARN') Fail on Level: WARN") />
		<cfset assertTrue(testLogger.isEnabledFor("ERROR"), "isEnabledFor('ERROR') Fail on Level: WARN") />
		<cfset assertTrue(testLogger.isEnabledFor("FATAL"), "isEnabledFor('FATAL') Fail on Level: WARN") />

		<!--- Set Logger to ERROR and Test --->
		<cfset testLogger.setLevel("ERROR") />

		<cfset assertEquals(testLogger.getLevel(), "ERROR", "getLevel() Fail on ERROR") />

		<cfset assertFalse(testLogger.isTraceEnabled(), "isTraceEnabled() Fail on Level: ERROR") />
		<cfset assertFalse(testLogger.isDebugEnabled(), "isDebugEnabled() Fail on Level: ERROR") />
		<cfset assertFalse(testLogger.isInfoEnabled(), "isInfoEnabled() Fail on Level: ERROR") />
		<cfset assertFalse(testLogger.isEnabledFor("WARN"), "isEnabledFor('WARN') Fail on Level: ERROR") />
		<cfset assertTrue(testLogger.isEnabledFor("ERROR"), "isEnabledFor('ERROR') Fail on Level: ERROR") />
		<cfset assertTrue(testLogger.isEnabledFor("FATAL"), "isEnabledFor('FATAL') Fail on Level: ERROR") />

		<!--- Set Logger to FATAL and Test --->
		<cfset testLogger.setLevel("FATAL") />

		<cfset assertEquals(testLogger.getLevel(), "FATAL", "getLevel() Fail on FATAL") />

		<cfset assertFalse(testLogger.isTraceEnabled(), "isTraceEnabled() Fail on Level: FATAL") />
		<cfset assertFalse(testLogger.isDebugEnabled(), "isDebugEnabled() Fail on Level: FATAL") />
		<cfset assertFalse(testLogger.isInfoEnabled(), "isInfoEnabled() Fail on Level: FATAL") />
		<cfset assertFalse(testLogger.isEnabledFor("WARN"), "isEnabledFor('WARN') Fail on Level: FATAL") />
		<cfset assertFalse(testLogger.isEnabledFor("ERROR"), "isEnabledFor('ERROR') Fail on Level: FATAL") />
		<cfset assertTrue(testLogger.isEnabledFor("FATAL"), "isEnabledFor('FATAL') Fail on Level: FATAL") />

		<!--- Set Logger to OFF and Test --->
		<cfset testLogger.setLevel("OFF") />

		<cfset assertEquals(testLogger.getLevel(), "OFF", "getLevel() Fail on OFF") />

		<cfset assertFalse(testLogger.isTraceEnabled(), "isTraceEnabled() Fail on Level: OFF") />
		<cfset assertFalse(testLogger.isDebugEnabled(), "isDebugEnabled() Fail on Level: OFF") />
		<cfset assertFalse(testLogger.isInfoEnabled(), "isInfoEnabled() Fail on Level: OFF") />
		<cfset assertFalse(testLogger.isEnabledFor("WARN"), "isEnabledFor('WARN') Fail on Level: OFF") />
		<cfset assertFalse(testLogger.isEnabledFor("ERROR"), "isEnabledFor('ERROR') Fail on Level: OFF") />
		<cfset assertFalse(testLogger.isEnabledFor("FATAL"), "isEnabledFor('FATAL') Fail on Level: OFF") />
	</cffunction>

	<cffunction name="LoggerShouldThrowExceptionIfInvalidLogLevelIsUsed" access="public" returntype="void" output="false">
		<cftry>
			<cfset testLogger.setLevel("badLevel") />
			<cfcatch type="cfesapi.org.owasp.esapi.errors.loggerException">
				<cfset assertTrue(true) />
			</cfcatch>
		</cftry>
	</cffunction>

	<cffunction name="LoggerShouldBeAbleLogTrace" access="public" returntype="void" output="false">
		<cftry>
			<cfset testLogger.trace("TRACE Message") />

			<cfcatch type="Any">
				<cfset fail("trace() failed")>
			</cfcatch>
		</cftry>
	</cffunction>

	<cffunction name="LoggerShouldBeAbleLogDebug" access="public" returntype="void" output="false">
		<cftry>
			<cfset testLogger.debug("DEBUG Message") />

			<cfcatch type="Any">
				<cfset debug(cfcatch) />
				<cfset fail("debug() failed") />
			</cfcatch>
		</cftry>
	</cffunction>

	<cffunction name="LoggerShouldBeAbleLogInfo" access="public" returntype="void" output="false">
		<cftry>
			<cfset testLogger.info("INFO Message") />

			<cfcatch type="Any">
				<cfset fail("info() failed")>
			</cfcatch>
		</cftry>
	</cffunction>

	<cffunction name="LoggerShouldBeAbleLogWarn" access="public" returntype="void" output="false">
		<cftry>
			<cfset testLogger.warn("WARN Message") />

			<cfcatch type="Any">
				<cfset fail("warn() failed")>
			</cfcatch>
		</cftry>
	</cffunction>

	<cffunction name="LoggerShouldBeAbleLogError" access="public" returntype="void" output="false">
		<cftry>
			<cfset testLogger.error("ERROR Message") />

			<cfcatch type="Any">
				<cfset fail("error() failed")>
			</cfcatch>
		</cftry>
	</cffunction>

	<cffunction name="LoggerShouldBeAbleLogFatal" access="public" returntype="void" output="false">
		<cftry>
			<cfset testLogger.fatal("FATAL Message") />

			<cfcatch type="Any">
				<cfset fail("fatal() failed")>
			</cfcatch>
		</cftry>
	</cffunction>
</cfcomponent>
